package com.cloud.code.真题;

import java.util.*;

/**
 * 给定一个射击比赛成绩单
 */
public class Demo3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            int n = Integer.parseInt(scanner.nextLine());
            String[] split1 = scanner.nextLine().split(",");
            String[] split2 = scanner.nextLine().split(",");
            int[] nums = new int[n];
            int[] scotes = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = Integer.parseInt(split1[i]);
                scotes[i] = Integer.parseInt(split2[i]);
            }
            Map<Integer, List<Integer>> map = new HashMap<>();

            for (int i = 0; i < nums.length; i++) {
                List<Integer> list = map.get(nums[i]);
                if (list == null) {
                    List<Integer> list1 = new ArrayList<>();
                    list1.add(scotes[i]);
                    map.put(nums[i], list1);
                } else {
                    list.add(scotes[i]);
                    map.put(nums[i], list);
                }
            }

            Iterator<Map.Entry<Integer, List<Integer>>> iterator = map.entrySet().iterator();

            while (iterator.hasNext()) {
                Map.Entry<Integer, List<Integer>> next = iterator.next();
                if (next.getValue().size() < 3) {
                    iterator.remove();
                } else {
                    Collections.sort(next.getValue());
                }
            }

            List<Map.Entry<Integer, List<Integer>>> list = new ArrayList<>(map.entrySet());
            list.sort(new Comparator<Map.Entry<Integer, List<Integer>>>() {
                @Override
                public int compare(Map.Entry<Integer, List<Integer>> o1, Map.Entry<Integer, List<Integer>> o2) {
                    List<Integer> value1 = o1.getValue();
                    List<Integer> value2 = o2.getValue();

                    int sum1 = value1.get(value1.size() - 1) + value1.get(value1.size() - 2) + value1.get(value1.size() - 3);
                    int sum2 = value2.get(value2.size() - 1) + value2.get(value2.size() - 2) + value2.get(value2.size() - 3);

                    if (sum1 == sum2) {
                        return o2.getKey() - o1.getKey();
                    } else {

                        return sum2 - sum1;
                    }

                }
            });

            StringBuilder string = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                if (i != list.size()-1) {
                    string.append(list.get(i).getKey()+",");
                } else {
                    string.append(list.get(i).getKey());
                }

            }

            System.out.println(string);

        }
    }
}
